$import( "SmitearWebGL.Events.EventDispatcher" );

SmitearWebGL.Objects.Parser.Collada.DaeEntity = SmitearWebGL.Events.EventDispatcher.extend(
{
	/******** constructor ********/

	init : function( document, node )
	{
		this.id = "";
		this.name = "";
		this.sid = "";
		this.asset = null;
		this.extras = null;
		this.document = document;
		
		this._super();

		if ( node != undefined && node != null ) this.read( node );
		this._registClass( "SmitearWebGL.Objects.Parser.Collada.DaeEntity" );
	}
	,
	
	/******** public method ********/
	
	destroy : function()
	{
		this.document = null;
	}
	,
	getAttributeAsFloat : function( node, name, defaultValue )
	{
		defaultValue = defaultValue == undefined ? 0 : defaultValue;

		var attr = this.getAttribute( node, name );
		return (isNaN(parseFloat(attr)) ? defaultValue : parseFloat(attr));
	}
	,
	getAttributeAsInt : function( node, name, defaultValue )
	{
		defaultValue = defaultValue == undefined ? 0 : defaultValue;

		var attr = this.getAttribute( node, name );
		return (isNaN(parseInt(attr, 10)) ? defaultValue : parseInt(attr, 10));
	}
	,
	getAttribute : function( node, name, stripPound )
	{
		stripPound = stripPound == undefined ? true : stripPound;

		var attr = node.getAttribute( name );
		var ret = attr != null ? attr.toString() : "";
		if( stripPound && ret.indexOf("#") == 0 )
		ret = ret.split("#")[1];
		return ret;
	}
	,
	getBools : function( node )
	{
		var arr = this.getStrings( node );
		for( var i = 0; i < arr.length; i++ )
		arr[i] = (arr[i] == "true" ? true : false);
		return arr;
	}
	,
	getFloats : function( node )
	{
		var arr = this.getStrings( node );
		for( var i = 0; i < arr.length; i++ )
		{
			var s = arr[i];
			s = s.replace(/,/, ".");
			arr[i] = parseFloat(s);
		}
		return arr;
	}
	,
	getInts : function( node )
	{
		var arr = this.getStrings( node );
		for( var i = 0; i < arr.length; i++ )
		arr[i] = parseInt( arr[i].trim(), 10 );
		return arr;
	}
	,
	getStrings : function( node )
	{
		return node.childNodes[0].nodeValue.trim().split(/\s+/);
	}
	,
	getNode : function( parent, nodeName )
	{
		var node = parent.getElementsByTagName( nodeName )[0];
		return node == undefined ? null : node;
	}
//	,
//	getNodeById : function( parent, nodeName, id )
//	{
//		return parent..collada::[nodeName].(@[SmitearWebGL.Objects.Parser.Collada.Const.DAE_ID_ATTRIBUTE] == id)[0];
//	}
	,
	getNodeContent : function( parent )
	{
		return ( parent ? parent.childNodes[0].nodeValue : "" );
	}
	,
	getNodeList : function( parent, nodeName )
	{
		return parent.getElementsByTagName( nodeName );
	}
	,
	read : function( node )
	{
		this.extras = {};
		this.id = this.getAttribute( node, SmitearWebGL.Objects.Parser.Collada.Const.DAE_ID_ATTRIBUTE );
		this.name = this.getAttribute( node, SmitearWebGL.Objects.Parser.Collada.Const.DAE_NAME_ATTRIBUTE );
		this.sid = this.getAttribute( node, SmitearWebGL.Objects.Parser.Collada.Const.DAE_SID_ATTRIBUTE );
	}
	,
	write : function( indent )
	{
		return indent == undefined ? "" : indent;
	}
	,
	writeSimpleEndElement : function( nodeName, indent )
	{
		indent = indent == undefined ? "" : indent;
		
		return indent + '</' + nodeName + '>\n';
	}
	,
	writeSimpleStartElement : function( nodeName, indent )
	{
		indent = indent == undefined ? "" : indent;
		
		return indent + '<' + nodeName + '>\n';
	}
}
);